package org.walkframework.sample.business.sample.adapter.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.walkframework.boot.bean.RespInfo;
import org.walkframework.boot.ehdb.bean.PageResult;
import org.walkframework.sample.business.sample.adapter.web.request.SamplePageQueryRequest;
import org.walkframework.sample.business.sample.app.SimpleCrudService;
import org.walkframework.sample.business.sample.infra.dataobject.Sample;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 简易业务测试服务
 *
 * @author 尾生
 * @since 2024/4/9
 */
@RestController
@RequestMapping("/crud")
public class SimpleCrudController {

    @Autowired
    private SimpleCrudService simpleCrudService;

    @RequestMapping("/insert")
    public RespInfo<Long> insert(@RequestBody Sample sample) {
        return RespInfo.successWithData(simpleCrudService.insert(sample));
    }

    @RequestMapping("/delete")
    public RespInfo<Boolean> delete(@RequestParam Long id) {
        return RespInfo.successWithData(simpleCrudService.delete(id));
    }

    @RequestMapping("/update")
    public RespInfo<Boolean> update(@RequestBody Sample sample) {
        return RespInfo.successWithData(simpleCrudService.update(sample));
    }

    @RequestMapping("/get")
    public RespInfo<Sample> get(@RequestParam Long id) {
        return RespInfo.successWithData(simpleCrudService.get(id));
    }

    @RequestMapping("/list")
    public RespInfo<List<Sample>> list(@RequestBody Sample sample) {
        return RespInfo.successWithData(simpleCrudService.list(sample));
    }

    /**
     * 分页查询，验证分页请求校验
     *
     * @param sample
     * @return
     */
    @RequestMapping("/page")
    public RespInfo<PageResult<Sample>> listPage(@RequestBody @Valid SamplePageQueryRequest sample) {
        return RespInfo.successWithData(simpleCrudService.page(sample));
    }

    @RequestMapping("/pageBySqlForDO")
    public RespInfo<PageResult<Sample>> listPageBySqlForDO(@RequestBody SamplePageQueryRequest sample) {
        return RespInfo.successWithData(simpleCrudService.pageBySqlForDO(sample));
    }

    @RequestMapping("/pageBySql")
    public RespInfo<PageResult<Map<String, Object>>> listPageBySql(@RequestBody SamplePageQueryRequest sample) {
        return RespInfo.successWithData(simpleCrudService.pageBySql(sample));
    }

}
